package com.ffcs.cn.action.desktopcontrol.colorSelectionPopupMenu;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.colorSelect.ColorSelectionPopupMenu;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;


public class DialogColorSelectionPopupMenu extends SmDialog {
	private JButton buttonColorSelection;
	private ColorSelectionPopupMenu selectionPopupMenu;
	private JPanel result;


	public DialogColorSelectionPopupMenu() {
		setTitle("ColorSelectionPopupMenu 示例");
		setSize(300,240);
		initComponents();
		initLayout();
		initListener();
	}

	private void initListener() {
		buttonColorSelection.addActionListener(e -> {
			//设置显示位置
			selectionPopupMenu.show(buttonColorSelection,buttonColorSelection.getWidth(),buttonColorSelection.getHeight());
		});
		//监听颜色改变
		selectionPopupMenu.addColorChangedListener(colorChangedEvent -> {
			//获取改变颜色
			Color color = colorChangedEvent.getColor();
			if (color != null){
				result.setBackground(color);
			}
		});
	}

	private void initLayout() {
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder("控件"));
		panel.add(buttonColorSelection);
		this.setLayout(new GridBagLayout());
		this.add(panel, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP
						, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH));

		result = new JPanel();
		result.setPreferredSize(new Dimension(100,100));
		result.setMinimumSize(new Dimension(100,100));
		JPanel resultParentPanel = new JPanel();
		resultParentPanel.add(result);
		resultParentPanel.setBorder(new TitledBorder("颜色选中结果"));
		this.add(resultParentPanel, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP
						, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH));
	}

	private void initComponents() {
		buttonColorSelection = new JButton("颜色选择器");
		selectionPopupMenu = new ColorSelectionPopupMenu();
	}
}
